package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.User;
import com.service.imp.UserService;
import com.utils.JsonToClazz;
import com.vo.Clazz;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.json.JSONArray;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * Created by head79 on 2017/6/19.
 */
@Controller
//@Scope("prototype")
//Springmvc controller默认是单例多线程的，为了提高性能
//尽量不要定义成员变量跟类变量，尽量使用局部变量，以免造成线程不安全
//如若非要使用成员变量，可以更改此controller的作用域@Scope("prototype")，使其成为多例
public class userController  {
    @Autowired
    private UserService userService;
    @ResponseBody
    @RequestMapping(value = "/showUser",method = RequestMethod.GET)
    public ModelMap showUser(HttpServletRequest request, HttpServletResponse response){
        ServletContext application = request.getServletContext();
        String context = application.getInitParameter("contextConfigLocation");
        String name = request.getParameter("name");
        List list=userService.showAllUser();
        ModelMap modelMap =new ModelMap();
        modelMap.put("msg",name);
        modelMap.put("success","true");
        System.out.println(JSON.toJSONString(list, SerializerFeature.WriteMapNullValue));
        return modelMap;

    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@Valid User user, BindingResult bindingResult, RedirectAttributes redirectAttributes){
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        Map<String, Object> map = new HashMap<String, Object>();
//        if (username != null)
//            map.put("code", "0");
//         else
//             map.put("code", "1");
            try {
                if(bindingResult.hasErrors())
                    return "login";
                SecurityUtils.getSubject().login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));
                return "redirect:/login";
            }catch (AuthenticationException e ){
                redirectAttributes.addFlashAttribute("message","用户名或密码错误!");
                return "redirect:/go";
            }
    }
    @RequestMapping(value = "/go",method = RequestMethod.GET)
    public ModelAndView go(){
        return new ModelAndView("show");
    }

    @RequestMapping(value = "/showUser",method = RequestMethod.POST)
    public ModelAndView doShow( HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        ModelAndView mvc = new ModelAndView();
        mvc.setViewName("show");
        ModelMap mm = new ModelMap();
        mm.put("success","true");
        mvc.addObject(mm);
        return mvc;
    }
    @ResponseBody
    @RequestMapping(value = "/showJson" ,method = RequestMethod.POST)
    public ModelAndView showJson( HttpServletRequest request,@RequestParam String subject,@RequestParam String source ) throws InstantiationException, IllegalAccessException {
        JsonToClazz.toClazz(Clazz.class,subject,source);
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @Test
    public void  test(){
        for(int i=0;i<60;i++){
            UUID uuid = UUID.randomUUID();
            System.out.println(uuid.toString().replace("-",""));
        }

    }
}
